/**
 * @file ErrorHandler.cpp
 * @brief 错误处理模块实现
 */

#include "CLink/error/ErrorHandler.h"
#include <sstream>
#include <iostream>

namespace CLink
{
namespace Error
{

// CLinkException 实现
CLinkException::CLinkException(ErrorCode code, const std::string &message, const std::string &detail)
    : code_(code), message_(message), detail_(detail)
{
}

ErrorCode CLinkException::getCode() const
{
    return code_;
}

const std::string &CLinkException::getMessage() const
{
    return message_;
}

const std::string &CLinkException::getDetail() const
{
    return detail_;
}

const char *CLinkException::what() const noexcept
{
    if (fullMessage_.empty())
    {
        std::ostringstream oss;
        oss << "Error " << static_cast<int>(code_) << " [" << ErrorHandler::getErrorDescription(code_) << "]: " << message_;
        if (!detail_.empty())
        {
            oss << " - " << detail_;
        }
        fullMessage_ = oss.str();
    }
    return fullMessage_.c_str();
}

// ErrorHandler 实现
ErrorHandler::ErrorHandler() : globalCallback_(nullptr)
{
}

ErrorHandler::~ErrorHandler()
{
}

ErrorHandler &ErrorHandler::getInstance()
{
    static ErrorHandler instance;
    return instance;
}

void ErrorHandler::registerErrorCallback(ErrorCode code, ErrorCallback callback)
{
    errorCallbacks_[code] = callback;
}

void ErrorHandler::registerGlobalErrorCallback(ErrorCallback callback)
{
    globalCallback_ = callback;
}

void ErrorHandler::handleError(const CLinkException &exception)
{
    // 首先尝试查找特定错误代码的回调
    auto it = errorCallbacks_.find(exception.getCode());
    if (it != errorCallbacks_.end() && it->second)
    {
        it->second(exception);
        return;
    }

    // 如果没有找到特定错误代码的回调，尝试使用全局回调
    if (globalCallback_)
    {
        globalCallback_(exception);
        return;
    }

    // 如果没有注册任何回调，则输出错误信息到标准错误流
    std::cerr << "Unhandled exception: " << exception.what() << std::endl;
}

std::string ErrorHandler::getErrorDescription(ErrorCode code)
{
    switch (code)
    {
    // 通用错误
    case ErrorCode::SUCCESS:
        return "Success";
    case ErrorCode::UNKNOWN_ERROR:
        return "Unknown Error";
    case ErrorCode::INVALID_ARGUMENT:
        return "Invalid Argument";
    case ErrorCode::NOT_IMPLEMENTED:
        return "Not Implemented";
    case ErrorCode::OPERATION_TIMEOUT:
        return "Operation Timeout";

    // 网络错误
    case ErrorCode::NETWORK_ERROR:
        return "Network Error";
    case ErrorCode::CONNECTION_FAILED:
        return "Connection Failed";
    case ErrorCode::CONNECTION_TIMEOUT:
        return "Connection Timeout";

    // 认证错误
    case ErrorCode::AUTHENTICATION_FAILED:
        return "Authentication Failed";
    case ErrorCode::UNAUTHORIZED:
        return "Unauthorized";
    case ErrorCode::TOKEN_EXPIRED:
        return "Token Expired";

    // 数据错误
    case ErrorCode::DATA_ERROR:
        return "Data Error";
    case ErrorCode::DATA_NOT_FOUND:
        return "Data Not Found";
    case ErrorCode::DATA_ALREADY_EXISTS:
        return "Data Already Exists";
    case ErrorCode::DATA_VALIDATION_FAILED:
        return "Data Validation Failed";

    // 系统错误
    case ErrorCode::SYSTEM_ERROR:
        return "System Error";
    case ErrorCode::RESOURCE_EXHAUSTED:
        return "Resource Exhausted";
    case ErrorCode::INTERNAL_ERROR:
        return "Internal Error";

    default:
        return "Unknown Error Code";
    }
}

// 辅助函数实现
void throwError(ErrorCode code, const std::string &message, const std::string &detail)
{
    throw CLinkException(code, message, detail);
}

} // namespace Error
} // namespace CLink